package com.wyh.contorller;

import com.wyh.entity.Account;
import com.wyh.entity.RestBean;
import com.wyh.entity.User_Name;
import com.wyh.mapper.UserMapper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Validated
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    UserMapper usermapper;

    @Resource
    BCryptPasswordEncoder encoder;

    @DeleteMapping("/delete")
    public RestBean<String> deleteUser(String email) {
        Account account = usermapper.findAccountByNameOrEmail(email);
        if (account == null) {
            return RestBean.failure(401, "删除失败，账号不存在，请检查是否输入正确，或者请寻找管理员");
        } else {
            usermapper.deleteAccountByEmail(email); // 调用Mapper中的删除方法
        }
        return RestBean.success("null");
    }


    @PutMapping("/update")
    public RestBean<String> updateUser(@RequestBody Account account) {
        try {
            Account user = usermapper.findAccount(Integer.parseInt(String.valueOf(account.getUid())));

            user.setName(StringUtils.hasText(account.getName())?account.getName():user.getName());
            user.setEmail(StringUtils.hasText(account.getEmail())?account.getEmail():user.getEmail());
            user.setTelephoneNumber(StringUtils.hasText(account.getTelephoneNumber())? account.getTelephoneNumber():user.getTelephoneNumber());
            user.setCN_ID(StringUtils.hasText(account.getCN_ID())? account.getCN_ID():user.getCN_ID());
            user.setPermission(StringUtils.hasText(account.getPermission())? account.getPermission():user.getPermission());
            user.setGender(StringUtils.hasText(account.getGender())? account.getGender():user.getGender());
            user.setRemark(StringUtils.hasText(account.getRemark())? account.getRemark():user.getRemark());
            user.setUsername(StringUtils.hasText(account.getUsername())? account.getUsername():user.getUsername());
            user.setPassword(StringUtils.hasText(account.getPassword())?encoder.encode(account.getPassword()):user.getPassword());

            usermapper.updateAccount(user);
            return RestBean.success("用户信息修改成功！");
        } catch (Exception e) {
            return RestBean.failure(400, "aaa,用户名或邮箱已注册");
        }
    }


    @PostMapping("/add")
    public RestBean<String> addUser(@RequestBody Account account) {

        Account existingUser = usermapper.findAccountByNameOrEmail(account.getUsername());
        if (existingUser == null) {
            Account user = new Account();
            user.setEmail(account.getEmail());
            user.setName(account.getName());
            user.setTelephoneNumber(account.getTelephoneNumber());
            user.setCN_ID(account.getCN_ID());
            user.setPermission(account.getPermission());
            user.setGender(account.getGender());
            user.setRemark(account.getRemark());
            user.setUsername(account.getUsername());
            user.setPassword(encoder.encode(account.getPassword()));

            try {
                System.out.println(user);
                usermapper.addUser(user);

                return RestBean.success("用户信息添加成功！");
            } catch (Exception e) {
                e.printStackTrace();
                return RestBean.failure(500, "添加用户时发生错误：" + e.getMessage());
            }
        }
        return RestBean.failure(400, "用户名或邮箱已注册，或者还有必填项未填");
    }

    @GetMapping("/getAll")
    public RestBean<List<Account>> getAllUser() {
        List<Account> userList = usermapper.findAll(); // 假设findAll()返回List<User>
        return RestBean.success(userList); // 调用RestBean.success()的接受列表参数的版本
    }

    @GetMapping("/getAllName")
    public RestBean<List<User_Name>> getAllName() {
        List<User_Name> list = usermapper.findNameOfAccount(); // 假设findAll()返回List<User>
        return RestBean.success(list); // 调用RestBean.success()的接受列表参数的版本
    }

}
